import Mock from 'mockjs'

// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}

export let List = [
    {
        id: 0,
        username: 'xiayujie',
        password: 'xiayujie',
        type: true,
        limit: '超级管理员'
    },
    {
        id: 1,
        username: '1476876027',
        password: '1476876027',
        type: true,
        limit: '管理员'
    },
    {
        id: 2,
        username: 'yydz',
        password: 'yydz',
        type: false,
        limit: '游客'
    },
    {
        id: 3,
        username: 'laoba',
        password: 'laoba',
        type: false,
        limit: '管理员'
    },
    {
        id: 4,
        username: 'xiaoxiao',
        password: 'xiaoxiao',
        type: true,
        limit: '游客'
    },
]

export default {
    /**
     * 获取列表
     * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
     * @param name, page, limit
     * @return {{code: number, count: number, data: *[]}}
     */
    getCountList: config => {
        const { name, page = 1, limit = 20 } = param2Obj(config.url)
        const mockList = List.filter(user => {
            if (name && user.name.indexOf(name) === -1 && user.addr.indexOf(name) === -1) return false
            return true
        })
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        return {
            code: 20000,
            count: mockList.length,
            list: pageList
        }
    },
    /**
     * 增加用户
     * @param name, addr, age, birth, sex
     * @return {{code: number, data: {message: string}}}
     */
    createAccount: config => {
        const { name, pass, buttonType, id } = JSON.parse(config.body)
        if (buttonType === 0) {
            console.log(List);
            List.unshift({
                username: name,
                password: pass,
                type: true,
                id: id,
                limit: '管理员'
            })
            return {
                code: 20000,
                data: {
                    message: '添加成功'
                }
            }
        }
        else if (buttonType === 1) {
            List.unshift({
                username: name,
                password: pass,
                type: true,
                id: id,
                limit: '游客'
            })
            return {
                code: 20000,
                data: {
                    message: '添加成功'
                }
            }
        }
    },
    /**
     * 删除用户
     * @param id
     * @return {*}
     */
    deleteAccount: config => {
        const { username } = JSON.parse(config.body)
        if (!username) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(u => u.username !== username)
            return {
                code: 20000,
                message: '删除成功'
            }
        }
    },
    /**
     * 修改用户
     * @param username, password,
     * @return {{code: number, data: {message: string}}}
     */
    updateAccount: config => {
        const { name, id, pass } = JSON.parse(config.body)
        List.some(u => {
            if (u.id === id) {
                u.username = name
                u.password = pass
                return true
            }
        })
        return {
            code: 20000,
            data: {
                message: '编辑成功'
            }
        }
    }
}
